package cn.echase.ecare.yinfayun.service.impl;

import cn.echase.common.core.enums.ResultCode;
import cn.echase.ecare.yinfayun.domain.dto.ElderStatisticsDto;
import cn.echase.ecare.yinfayun.domain.dto.StatisticsDto;
import cn.echase.ecare.yinfayun.domain.mapper.OrgBaseInfoStatisticsMapper;
import cn.echase.ecare.yinfayun.domain.vo.ElderAgeInfoVo;
import cn.echase.ecare.yinfayun.domain.vo.EldersAgeMaxAndMinVo;
import cn.echase.ecare.yinfayun.domain.vo.OrgElderCenterVo;
import cn.echase.ecare.yinfayun.service.IElderAgeStatisticService;
import cn.echase.ecare.yinfayun.service.ToolService;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 长者年龄数据统计Service业务层处理
 *
 * @author yinyy
 * @date 2024-11-07
 */
@Slf4j
@Service
public class ElderAgeStatisticServiceImpl implements IElderAgeStatisticService {

    @Autowired
    private ToolService toolService;

    @Resource
    private OrgBaseInfoStatisticsMapper statisticsMapper;

    private final static String KEY_AVG = "avg";
    private final static String KEY_OLDEST_AGE = "oldestAge";
    private final static String KEY_YOUNGEST_AGE = "youngestAge";
    private final static String KEY_MALE_AVG = "maleAvg";
    private final static String KEY_MALE_OLDEST_AGE = "maleOldestAge";
    private final static String KEY_MALE_YOUNGEST_AGE = "maleYoungestAge";
    private final static String KEY_FEMALE_AVG = "femaleAvg";
    private final static String KEY_FEMALE_OLDEST_AGE = "femaleOldestAge";
    private final static String KEY_FEMALE_YOUNGEST_AGE = "femaleYoungestAge";

    /**
     * 查询机构长者年龄数据统计列表
     *
     * @param statisticsDto 查询条件
     * @return 机构照护等级数据统计列表
     */
    @Override
    public TableDataInfo<ElderAgeInfoVo> eldersAgeStatisticPageList(StatisticsDto statisticsDto) {
        PageHelper.startPage(statisticsDto.getPageNum(), statisticsDto.getPageSize());
        List<ElderAgeInfoVo> pageList = statisticsMapper.getBuildingElderAge(statisticsDto);
//        return TableDataInfo.build(ResultCode.SUCCESS.getCode(), pageList);

        return null; // TODO 需做兼容
    }

    /**
     * 查询机构年龄数据统计列表
     *
     * @param statisticsDto 查询条件
     * @return 机构照护等级数据统计列表
     */
    @Override
    public List<ElderAgeInfoVo> eldersAgeStatisticList(StatisticsDto statisticsDto) {
        return statisticsMapper.getBuildingElderAge(statisticsDto);
    }

    /**
     * 查询机构照护等级数据统计列表
     *
     * @param statisticsDto 查询条件
     * @return 机构照护等级数据统计列表
     */
    @Override
    public Map<String, Object> getElderAgeStatisticInfo(StatisticsDto statisticsDto) {
        Map<String, Object> resultMap = new HashMap<>();
        statisticsDto.setSex(null);
        EldersAgeMaxAndMinVo totalVo = statisticsMapper.getOrgElderAgeMaxAndMin(statisticsDto);
        statisticsDto.setSex(1);
        EldersAgeMaxAndMinVo maleVo = statisticsMapper.getOrgElderAgeMaxAndMin(statisticsDto);
        statisticsDto.setSex(2);
        EldersAgeMaxAndMinVo femaleVo = statisticsMapper.getOrgElderAgeMaxAndMin(statisticsDto);
        if (totalVo != null) {
            double maleAvgAge = maleVo != null ? maleVo.getAvgAge().doubleValue() : 0;
            double femaleAvgAge = femaleVo != null ? femaleVo.getAvgAge().doubleValue() : 0;
            double totalAvgAge = (maleAvgAge + femaleAvgAge) / 2;
            resultMap.put(KEY_AVG, new BigDecimal(totalAvgAge).setScale(1, RoundingMode.HALF_UP));
            resultMap.put(KEY_OLDEST_AGE, totalVo.getMaxAge());
            resultMap.put(KEY_YOUNGEST_AGE, totalVo.getMinAge());
        }

        if (maleVo != null) {
            resultMap.put(KEY_MALE_AVG, maleVo.getAvgAge().setScale(1, RoundingMode.HALF_UP));
            resultMap.put(KEY_MALE_OLDEST_AGE, maleVo.getMaxAge());
            resultMap.put(KEY_MALE_YOUNGEST_AGE, maleVo.getMinAge());
        }

        if (maleVo != null) {
            resultMap.put(KEY_FEMALE_AVG, femaleVo.getAvgAge().setScale(1, RoundingMode.HALF_UP));
            resultMap.put(KEY_FEMALE_OLDEST_AGE, femaleVo.getMaxAge());
            resultMap.put(KEY_FEMALE_YOUNGEST_AGE, femaleVo.getMinAge());
        }


        return resultMap;
    }


    /**
     * 年龄段/楼栋相应长者列表查询
     *
     * @param elderStatisticsDto 查询条件
     * @return 长者列表
     */
    @Override
    public TableDataInfo<OrgElderCenterVo> getElderAgeDetailList(ElderStatisticsDto elderStatisticsDto) {
        PageHelper.startPage(elderStatisticsDto.getPageNum(), elderStatisticsDto.getPageSize());
        List<OrgElderCenterVo> pageList = statisticsMapper.getElderAgeDetailList(elderStatisticsDto);
        if (CollUtil.isEmpty(pageList)) {
//            return TableDataInfo.build(ResultCode.SUCCESS.getCode(), pageList);

            return null; // TODO 需做兼容
        }else {
            boolean singleBuilding = toolService.isSingleBuilding();
            if (singleBuilding) {
                for (OrgElderCenterVo vo : pageList) {
                    String splitBedName = toolService.splitBedName(vo.getBedName(), null);
                    vo.setBedName(splitBedName);
                }
            }
        }

//        return TableDataInfo.build(ResultCode.SUCCESS.getCode(), pageList);

        return null; // TODO 需做兼容
    }

}
